C # NPOCO lightweight ORM framework (Getting Started ),
Currently, this framework cannot be used to search Chinese documents.
Only the English wiki is available. Take an excerpt from translation learning.
Most of them are machine flip, and some places have been changed,
Because bloggers are also low-level, and the red ones are blind to understanding.
There may be errors. You can point them out if you find them.
Wiki address: http://github.com/schotime/NPoco/wiki
First query:
1 public class Use
First, define an interface named idatabase. In this interface, define the method name, parameters, and return values of database operations. In this case, I define the following methods:
Public interface idatabase
{
Bool connect (string connectstring );
Bool open ();
Bool command (string SQL );
Void close ();
}
Important Note: "The interface should be cautious throughout its life, and its definition should
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using Autofac. Builder; Using Autofac. configuration; Using Autofac; /// By rhythmk Namespace Autofacapp{ Class Program{ Static Void Main ( String [] ARGs){ VaR Builder = New Containerbuilder ();Builder. registertype Builder. registertype // Using (VAR Container = builder. Build (containerbuildoptions. Default )) // { VaR Container = builder. Build (containerbuildoptions. Default );
registered to the tree.}}Five, adapter modeEncapsulates a variety of distinct function interfaces into a unified API.PHP database operation has mysql,mysqli,pdo three kinds, you can use the adapter mode unified into the same, so that different database operations, unified into the same API. A similar scenario also has the cache adapter,Different cache functions, such as MEMCACHE,REDIS,FILE,APC, can be unified into the same.First define an interface (there are several methods, and the correspond
is registered in the Assembly assembly.End StatementThis article mainly tells AUTOFAC in the comparison commonly used also relatively good registration way, and did not all registers the way to tell.It is highly recommended that you use module, each of which has its own module, so that a module has a fixed unknown that makes it easy to find registrations and dependencies in the project.Through this article, you can learn several common registration methods, as well as the simplest way to obtain
commandtext) { Console.WriteLine (string. Format ("' {0} ' is a update for SQL in {1}!", CommandText, Name)); } public void Delete (string commandtext) { Console.WriteLine (string. Format ("' {0} ' is a delete sql in {1}!", CommandText, Name)); } }4) Next create a database manager DatabaseManager.cs:public class Databasemanager { idatabase _database; Public Databasemanager (idatabase da
mode to unify the interface. Similarly, the cache scenario is that it is more convenient to change the cache policy (memcache, redis, apc.
Advantages:
The adapter adapts to the target through the adapter to achieve the goal of being transparent to the customer.
Sample code:
// Adaptation target. the specified interface will be adapted to the object to implement interface IDatabase {public function connect ($ host, $ username, $ password, $ database
1. Description
1. Adapter Mode: 可以将截然不同的函数接口封装成统一的API2. Practical application example, PHP database operation has MySQL, mysqli, Pdo3 species, can be unified into the same adapter mode. A similar scenario is a cache adapter that unifies different cache functions, such as MEMCACHE,REDIS,FILE,APC, into a consistent3. The following example can help you deal with various database link functions, MySQL mysqli, PDO random selection, operation is abstracted into a unified interface client does
to the target through the adapter to achieve the goal of being transparent to the customer.
Sample Code:
// Adaptation target. The specified interface will be adapted to the object to implement interface IDatabase {public function connect ($ host, $ username, $ password, $ database ); public function query ($ SQL);} // adapter class Mysql implements IDatabase {protected $ connect; public function connect (
the caching strategy (Memcache, Redis, APC).
Advantage:
The adapter is used by the adaptor to complete the adaptation target to achieve the purpose of transparent to the customer.
Example code:
Adaptation target, the specified interface will be implemented interface idatabase{public function Connect ($host, $username, $password, $database); Public Function query ($sql);} Adapter class Mysql implements idatabase
the Adapter role needs to be created to adapt the Adaptee to the Target interface.
Application scenarios:
For example, data operations include mysql, mysqli, pdo, sqlite, postgresql, etc. if you need to change the database in the environment generated, you can use the Adapter mode to unify the interface. Similarly, the cache scenario is that it is more convenient to change the cache policy (memcache, redis, apc.
Advantages:
The adapter adapts to the target through the adapter to achieve the goa
) {
Self:: $object [$alias]= $object;
}
static function Get ($name)
{
Return self:: $object [$name];
}
function _unset ($alias) {
Unset (self:: $object [$alias]);
}
}
Combined with factory methods
Class factory{
static function CreateDatabase () {
$db = Database::getinstance ();
Register::set (' db1 ', $db);
return $db;
}
}
Index calls directly
$db = Register::get (' db1 ');
Adapter mode
1. Adapter mode, which can encapsulate a completely different function interface into a unified API
2. P
to replace the database, you can use the adapter mode unified interface. The same is true of the cache scenario, which is more convenient to replace the caching strategy (Memcache, Redis, APC).
Advantage:
The adapter is used by the adaptor to complete the adaptation target to achieve the purpose of transparent to the customer.
Example code:
?
123456789101112131415161718192021st2223242526272829303132333435363738394041424344454647
Adaptation target, the specified interface will b
, You cannot delete or modify the const file. ifile_map () can be used to map the const file to the memory. Therefore, the const file is rarely used as a carrier for communication. The file size supported by ifilemgr_getinfo () and ifilemgr_getinfoex () cannot exceed 4 GB (earlier than brew3.x). The file name length is also limited, through ishell_getdeviceinfo (pishell, Pi ), pi-> wmaxpath, you can get the maximum length of the file name including the path.
environment generated, you can use the adapter mode to unify the interface. Similarly, the cache scenario is that it is more convenient to change the cache Policy (memcache, redis, apc.
Advantages:
The adapter adapts to the target through the adapter to achieve the goal of being transparent to the customer.
Sample Code:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// Adaptation target. T
. Generally, You cannot delete or modify the const file. ifile_map () can be used to map the const file to the memory. Therefore, the const file is rarely used as a carrier for communication. The file size supported by ifilemgr_getinfo () and ifilemgr_getinfoex () cannot exceed 4 GB (earlier than brew3.x). The file name length is also limited, through ishell_getdeviceinfo (pishell, Pi ), pi-> wmaxpath, you can get the maximum length of the file name including the path.
adaptor, in order to achieve the purpose of transparent use of the customer.
Sample code:
Adaptation targets, the specified interface will be implemented by the adapter object
interface idatabase
{public
function connect ($host, $username, $password, $ database);
Public Function query ($sql);
Adapter
class Mysql implements Idatabase
{
protected $connect;
Public function Connect ($host,
, we can use the following methods:Connectionmultiplexer con = connectionmultiplexer.connect ("localhost:6379"), Redis Server default port is 6379.To reuse the Connectionmultiplexer object, we can use the object by defining a static property and returning an instance of Connectionmultiplexer. Next we'll write a simple singleton pattern to get the Connectionmultiplexer object. The code is as follows: Public class redisconnection { privatestaticreadonlynew lazy"localhost:6379"));
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.